/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.dejax.service.impl;

import com.dejax.domain.DevopsJenkinsclient;
import com.dejax.utils.ValidationUtil;
import com.dejax.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.dejax.repository.DevopsJenkinsclientRepository;
import com.dejax.service.DevopsJenkinsclientService;
import com.dejax.service.dto.DevopsJenkinsclientDto;
import com.dejax.service.dto.DevopsJenkinsclientQueryCriteria;
import com.dejax.service.mapstruct.DevopsJenkinsclientMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.dejax.utils.PageUtil;
import com.dejax.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Richer
* @date 2022-01-05
**/
@Service
@RequiredArgsConstructor
public class DevopsJenkinsclientServiceImpl implements DevopsJenkinsclientService {

    private final DevopsJenkinsclientRepository devopsJenkinsclientRepository;
    private final DevopsJenkinsclientMapper devopsJenkinsclientMapper;

    @Override
    public Map<String,Object> queryAll(DevopsJenkinsclientQueryCriteria criteria, Pageable pageable){
        criteria.setFlag(0);
        Page<DevopsJenkinsclient> page = devopsJenkinsclientRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(devopsJenkinsclientMapper::toDto));
    }

    @Override
    public List<DevopsJenkinsclientDto> queryAll(DevopsJenkinsclientQueryCriteria criteria){
        criteria.setFlag(0);
        return devopsJenkinsclientMapper.toDto(devopsJenkinsclientRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DevopsJenkinsclientDto findById(String jenkinsid) {
        DevopsJenkinsclient devopsJenkinsclient = devopsJenkinsclientRepository.findById(jenkinsid).orElseGet(DevopsJenkinsclient::new);
        ValidationUtil.isNull(devopsJenkinsclient.getJenkinsid(),"DevopsJenkinsclient","jenkinsid",jenkinsid);
        return devopsJenkinsclientMapper.toDto(devopsJenkinsclient);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DevopsJenkinsclientDto create(DevopsJenkinsclient resources) {
        resources.setJenkinsid(IdUtil.simpleUUID());
        resources.setFlag(0);
        return devopsJenkinsclientMapper.toDto(devopsJenkinsclientRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DevopsJenkinsclient resources) {
        DevopsJenkinsclient devopsJenkinsclient = devopsJenkinsclientRepository.findById(resources.getJenkinsid()).orElseGet(DevopsJenkinsclient::new);
        ValidationUtil.isNull( devopsJenkinsclient.getJenkinsid(),"DevopsJenkinsclient","id",resources.getJenkinsid());
        devopsJenkinsclient.copy(resources);
        devopsJenkinsclientRepository.save(devopsJenkinsclient);
    }

    @Override
    public void deleteAll(String[] ids) {
        DevopsJenkinsclient updagtesource = new DevopsJenkinsclient();
        for (String jenkinsid : ids) {
            updagtesource.setFlag(1);
            updagtesource.setJenkinsid(jenkinsid);
            this.update(updagtesource);
        }
    }

    @Override
    public void download(List<DevopsJenkinsclientDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DevopsJenkinsclientDto devopsJenkinsclient : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("触手客户端", devopsJenkinsclient.getClientid());
            map.put("apiurl", devopsJenkinsclient.getApiurl());
            map.put("userid", devopsJenkinsclient.getUserid());
            map.put("apitoken", devopsJenkinsclient.getApitoken());
            map.put(" flag",  devopsJenkinsclient.getFlag());
            map.put(" notes",  devopsJenkinsclient.getNotes());
            map.put(" reguser",  devopsJenkinsclient.getReguser());
            map.put(" regtime",  devopsJenkinsclient.getRegtime());
            map.put(" moduser",  devopsJenkinsclient.getModuser());
            map.put(" modtime",  devopsJenkinsclient.getModtime());
            map.put("描述", devopsJenkinsclient.getClientDesc());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}